package com.fms.trollalarm.service;

import java.util.Calendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import com.fms.trollalarm.AlarmActivity;
import com.fms.trollalarm.controller.AlarmController;
import com.fms.trollalarm.object.AlarmModel;
import com.fms.trollalarm.utils.Common.IntentExtra;

public class OnAlarmReceiver extends BroadcastReceiver {
	private AlarmController alarmController;
	private AlarmModel alarm;

	@Override
	public void onReceive(Context context, Intent intent) {
		alarmController = new AlarmController(context);
		Calendar localCalendar = Calendar.getInstance();
		this.alarm = alarmController.getAlarm(intent.getExtras().getLong(
				IntentExtra.ID));
		Log.i("fms", "alarm onReceive");
		if (!alarm.isRepeatWeekly()
				|| (alarm.isRepeatWeekly() && alarm
						.getRepeatingDay(localCalendar
								.get(Calendar.DAY_OF_WEEK) - 1))) {
			Intent newIntent = new Intent(context, AlarmActivity.class);
			newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			newIntent.putExtra(IntentExtra.ID, alarm.getId());
			context.startActivity(newIntent);
			//wakeLock.release();
		}
	}

}
